package appSimulator;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;

import environment.Application;

public class JMSProducer {

	private Queue queue;
	private Session session;
	private Connection connection;
	
	public JMSProducer() throws Exception {
		// create JMS queue
		String url = "tcp://localhost:61616";
		ConnectionFactory factory = new ActiveMQConnectionFactory(url);
		connection = factory.createConnection();
		session = connection.createSession(false,
			Session.AUTO_ACKNOWLEDGE);
		
		connection.start();
		queue = session.createQueue("lsdc_queue");
	}
	
	public void sendMessage(Application app) throws Exception
	{
		MessageProducer messageProducer = session.createProducer(queue);
		ObjectMessage message = session.createObjectMessage();
		message.setObject(app);
		messageProducer.send(message);
	}
	
	public void close() throws Exception
	{
		connection.close();
	}
}
